package com.example.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.Result;
import com.example.reggie.domain.ShoppingCart;
import com.example.reggie.domain.User;
import com.example.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/shoppingCart")
@Slf4j
@RestController
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    // 查询用户购物车列表
    @GetMapping("/list")
    Result<List> shoppingCartList(HttpServletRequest request){
        Object userId = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return Result.success(list);
    }

    // 用户添加购物车
    @PostMapping("/add")
    Result<String> shoppingCartAdd(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request){
        Long userId =(Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (shoppingCart.getSetmealId()!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        if (shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }

        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        if(shoppingCart1!=null){
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartService.updateById(shoppingCart1);
            return Result.success("添加成功");
        }
        shoppingCart.setUserId(userId);
        shoppingCartService.save(shoppingCart);
        return Result.success("添加成功");
    }

    // 用户删除购物车
    @PostMapping("/sub")
    Result<String> shoppingCartSub(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request){
        Long userId =(Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (shoppingCart.getSetmealId()!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        if (shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        if(shoppingCart1!=null){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            if (shoppingCart1.getNumber()==0)
            {
                shoppingCartService.removeById(shoppingCart1);
                return Result.success("删除成功");
            }
            shoppingCartService.updateById(shoppingCart1);
            return Result.success("删除成功");
        }
        return Result.success("删除失败");
    }

    // 清空购物车
    @DeleteMapping("/clean")
    Result<String> shoppingCartClear(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return Result.success("清空成功");
    }

}
